function [ww,eta]=update_w_CF_FP_bisection(N,K,MG,hGG,uu,vv,pow_max)
ww = zeros(MG,N,K);
eta = zeros(N,1);
for nn=1:N
    eta_min = 0;
    eta_max = 0.1;
    f_min = 0;
    f_max = pow_max(nn)*2;
    n_iter2 = 0;
    
    while abs(f_max - f_min) > 1e-2 && n_iter2 < 100
        eta_ = eta_min;
        sumsum = 0;
        for kk=1:K
            ww(:,nn,kk) = cal_ww(N,K,MG,hGG,eta_,uu,vv,nn,kk);
            sumsum = sumsum + real(ww(:,nn,kk)'*ww(:,nn,kk));
        end
        f_min = sumsum;
        
        eta_ = eta_max;
        sumsum = 0;
        for kk=1:K
            ww(:,nn,kk) = cal_ww(N,K,MG,hGG,eta_,uu,vv,nn,kk);
            sumsum = sumsum + real(ww(:,nn,kk)'*ww(:,nn,kk));
        end
        f_max = sumsum;
        
        eta_mid = (eta_max + eta_min) / 2;
        eta_ = eta_mid;
        sumsum = 0;
        for kk=1:K
            ww(:,nn,kk) = cal_ww(N,K,MG,hGG,eta_,uu,vv,nn,kk);
            sumsum = sumsum + real(ww(:,nn,kk)'*ww(:,nn,kk));
        end
        f_mid = sumsum;
        
        % real(ww(:,nn,kk)'*ww(:,nn,kk)) 是关于 eta 的单调递减函数
        % sum_k {real(ww(:,nn,kk)'*ww(:,nn,kk))} == pow_max(nn)
        % 当 eta < eta^* 时，基站发射功率(eta) > 基站最大发射功率
        % 当 eta = eta^* 时，基站发射功率(eta) = 基站最大发射功率
        % 当 eta > eta^* 时，基站发射功率(eta) < 基站最大发射功率
        if f_mid > pow_max(nn)
            eta_min = eta_mid;
        else
            eta_max = eta_mid;
        end
        n_iter2 = n_iter2 + 1;
    end
    eta(nn,1) = eta_mid;
end
end


function wnk = cal_ww(N,K,MG,hGG,eta,uu,vv,nn,kk)
denomi_W = zeros(MG,MG);
for mm=1:N
    for ll=1:K
        denomi_W = denomi_W + hGG{nn,mm,ll}*(vv(mm,ll)*vv(mm,ll)')*hGG{nn,mm,ll}';
    end
end
denomi_W = denomi_W + eta*eye(MG);
%         W{nn,kk} = inv(denominator_W) * hGG{nn,nn,kk}*V{nn,kk} * sqrt(weight{nn,kk}*(1+U{nn,kk}));
wnk = denomi_W \ (hGG{nn,nn,kk}*vv(nn,kk) * sqrt((1+uu(nn,kk))));
end


%     eta_ = 0:1e-8:1e-7;
%     power_w = zeros(1,length(eta_));
%     for ii = 1:length(eta_)
%         sumsum = 0;
%         for kk=1:K
%             ww(:,nn,kk) = cal_ww(N,K,MG,hGG,eta_(ii),uu,vv,nn,kk);
%             sumsum = sumsum + real(ww(:,nn,kk)'*ww(:,nn,kk));
%         end
%         power_w(ii) = sumsum;
%     end
%     figure
%     plot(eta_,power_w);
%     xlabel('eta')
%     ylabel('power_w')
%     grid on
%     hhhhh